<script>
    import { goto } from '$app/navigation';
    import { acceptInvite } from '$lib/apiServer';

    import LogIn from '$lib/components/LogIn.svelte';

    export let data;

    let disabled;
    let username;
    let password;

    async function onSubmit() {
        disabled = true;
        const response = await acceptInvite(data.invite.id, username, password);
        if (200 <= response.status && response.status < 300) {
            username = '';
            password = '';
            goto('/');
        }
        disabled = false;
    }
</script>

{#await data}
<p>Loading invitation…</p>
{:then { invite }}
<p>Hi there! {invite.issuer} invites you to the conversation.</p>
<LogIn bind:disabled bind:username bind:password on:submit={onSubmit} />
{/await}
